package com.ygou.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.qq.connect.QQConnectException;
import com.qq.connect.api.OpenID;
import com.qq.connect.api.qzone.UserInfo;
import com.qq.connect.javabeans.AccessToken;
import com.qq.connect.javabeans.qzone.UserInfoBean;
import com.qq.connect.oauth.Oauth;

@Controller
@RequestMapping(value="/qqlogin")
public class QQLoginController {
	
	@RequestMapping(value="/callqq")
    public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {  
        response.setContentType("text/html;charset=utf-8");  
        try {
        	response.sendRedirect(new Oauth().getAuthorizeURL(request));
        } catch (QQConnectException e) {  
            e.printStackTrace();  
        }  
    }
	
	@RequestMapping("/afterlogin") 
    public ModelAndView afterlogin(HttpServletRequest request, HttpServletResponse response) throws Exception {  
  
        try {  
            AccessToken accessTokenObj = (new Oauth()).getAccessTokenByRequest(request);  
            String accessToken   = null,  
                   openID        = null;  
            long tokenExpireIn = 0L;  
            if (accessTokenObj.getAccessToken().equals("")) {  
                System.out.print("没有获取到响应参数");  
            }else{  
                accessToken = accessTokenObj.getAccessToken();  
                tokenExpireIn = accessTokenObj.getExpireIn();  
                OpenID openIDObj =  new OpenID(accessToken);  
                openID = openIDObj.getUserOpenID();  
                UserInfo qzoneUserInfo = new UserInfo(accessToken, openID);  
                UserInfoBean userInfoBean = qzoneUserInfo.getUserInfo();  
                String name = userInfoBean.getNickname();  
               System.out.println("欢迎你，" + name + "!");  
  
            }  
        }catch(Exception e){  
            e.printStackTrace();  
        }  
        return null;  
    }
}
